﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

using DM_Article.Model;

namespace DM_Article.Bll
{
    public class smallclassbll
    {
        public static int add(OleDbConnection conn, smallclass model)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "insert into smallclass(bigclassid,smallclassname)values('" + model.bigclassid + "','" + model.smallclassname + "')";
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static int update(OleDbConnection conn, smallclass model)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "update smallclass set bigclassid=" + model.bigclassid + ",smallclassname ='" + model.smallclassname + "' where smallclassid=" + model.smallclassid;
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static int delete(OleDbConnection conn, string smallclassid)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "delete from smallclass where smallclassid=" + smallclassid;
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static int deleteByBigclass(OleDbConnection conn, string bigclassid)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "delete from smallclass where bigclassid=" + bigclassid;
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static smallclass getSmallclassDetail(OleDbConnection conn, string smallclassid)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [bigclassid],[smallclassname] from smallclass where smallclassid=" + smallclassid;
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);

            smallclass model = new smallclass();
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["bigclassid"].ToString() != "")
                {
                    model.bigclassid = Convert.ToInt32(dt.Rows[0]["bigclassid"].ToString());
                }
                if (dt.Rows[0]["smallclassname"].ToString() != "")
                {
                    model.smallclassname = dt.Rows[0]["smallclassname"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public static DataTable getSmallclassTable(OleDbConnection conn)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [smallclassid],[bigclassid],[smallclassname] from smallclass";
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);
            return dt;
        }

        public static DataTable getSmallByBig(OleDbConnection conn, int bigclassid)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [smallclassid],[smallclassname] from smallclass where bigclassid=" + bigclassid;
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);
            return dt;
        }

        public static int getSmallClassIdbyName(OleDbConnection conn, string name)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [smallclassid] from smallclass where smallclassname='" + name + "'";
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);

            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["smallclassid"].ToString() != "")
                {
                    return Convert.ToInt32(dt.Rows[0]["smallclassid"].ToString());
                }
            }

            return 0; 
        }
    }
}
